// ValidationAspects
// Written by Mike Saunders
// Released under Microsoft Public License (Ms-PL)
using System;
using System.Text.RegularExpressions;
using ValidationAspects.Exceptions;
using ValidationAspects.Sdk;

namespace ValidationAspects.Factories
{
	public class MatchesRegex : ValidatorFactoryBase<string>
	{
		public string Regex { get; set; }
		public string Name { get; set; }

		#region Overrides of ValidatorFactoryBase<string>
		protected override Action<string, IValidationContext> ValidateAction
		{
			get
			{
				var regex = new Regex(Regex);
				return (v, c) => { if (v != null && !regex.IsMatch(v)) throw new ValidateMatchesRegexException(Regex, v) {Name = Name}; };
			}
		}
		#endregion
	}
}